package multimedia;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Displayable;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;

import ui.RecordAudioForm;


/**
 * @author Thiago Bruno Melo de Sales <BR>
 * email: <a href=mailto:thiagobruno@embedded.ufcg.edu.br>thiagobruno@embedded.ufcg.edu.br</a> <BR>
 */
public class ThreadRecordAudio implements Runnable {

	private Player playerAudio;
	private RecordControl recordControl;	
	private ByteArrayOutputStream outputAudio;	
	private RecordAudioForm previous;
	
	public ThreadRecordAudio(RecordAudioForm previous) {
		this.previous = previous;
	}
	
	public ByteArrayOutputStream getOutputAudio() {
		return outputAudio;
	}
	
	public Player getPlayerAudio() {
		return playerAudio;
	}
	
	public RecordControl getRecordControl() {
		return recordControl;
	}

	public void run() throws SecurityException {
		boolean isRecording = true;
		try {
			
			this.playerAudio = Manager.createPlayer("capture://audio");
			this.playerAudio.realize();

			this.recordControl = (RecordControl) this.playerAudio.getControl("RecordControl");
			this.outputAudio   = new ByteArrayOutputStream();
			this.recordControl.setRecordStream(this.outputAudio);
			
			this.recordControl.startRecord();
			this.playerAudio.start();
		} catch (MediaException me) {
			previous.midlet.error(me);
		} catch (IOException ioe) {
			previous.midlet.error(ioe);
		} catch (SecurityException se) {
			isRecording = false;
			previous.midlet.error(se);
		} finally {
			//if(isRecording)
				//this.formRecordAudio.beginRecordTasks();
		}
	}

}
